package cn.stylefeng.guns.modular.recy.controller;
/*
 */

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.service.UserService;
import cn.stylefeng.guns.modular.weixin.utils.Utils;
import cn.stylefeng.roses.core.base.controller.*;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.reqres.response.*;
import cn.stylefeng.guns.modular.recy.entity.*;
import java.io.*;
import cn.stylefeng.guns.core.common.page.*;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.stylefeng.guns.modular.recy.vo.*;
import cn.stylefeng.guns.modular.recy.utils.*;
import cn.hutool.core.bean.*;
import java.util.stream.*;
import cn.stylefeng.guns.modular.recy.warpper.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.*;

@Controller
@RequestMapping({ "/integrateInfo" })
public class IntegrateInfoController extends BaseController
{
    private String PREFIX;
    @Autowired
    private IntegrateInfoService integrateInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private RegionService regionService;

    @Autowired
    private WasteTypeService wasteTypeService;

    public IntegrateInfoController() {
        this.PREFIX = "/modular/recy/integrateInfo/";
    }
    
    @RequestMapping({ "" })
    public String index(Model model) {
        List<User> userIdList = this.userService.list();
        model.addAttribute("userIdList",userIdList);

        return this.PREFIX + "integrateInfo.html";
    }
    
    @RequestMapping({ "/add" })
    public String integrateInfoAdd(Model model) {
        List<WasteType> wasteTypeList = this.wasteTypeService.list();
        model.addAttribute("wasteTypeList",wasteTypeList);
        return this.PREFIX + "integrateInfo_add.html";
    }
    
    @RequestMapping({ "/edit" })
    public String integrateInfoUpdate(@RequestParam("id") final String id) {
        return this.PREFIX + "integrateInfo_edit.html";
    }
    
    @RequestMapping({ "/detail/{id}" })
    @ResponseBody
    public ResponseData detail(@PathVariable final String id) {
        final IntegrateInfo integrateInfo = (IntegrateInfo)this.integrateInfoService.getById((Serializable)id);
        return (ResponseData)ResponseData.success(integrateInfo);
    }
    
    @RequestMapping({ "/list" })
    @ResponseBody
    public Object list(final IntegrateInfo integrateInfoQuery) {
        final QueryWrapper<IntegrateInfo> integrateInfoQueryWrapper = ControllerUtil.createQuery(integrateInfoQuery, this.getHttpServletRequest());
        final Page<IntegrateInfo> page = (Page<IntegrateInfo>)LayuiPageFactory.defaultPage();
        final IPage<IntegrateInfo> page2 = (IPage<IntegrateInfo>)this.integrateInfoService.page((IPage)page, (Wrapper)integrateInfoQueryWrapper);
        final List<IntegrateInfoVO> integrateInfoVOList = EntityBind.bind(page2.getRecords(), IntegrateInfoVO.class);
        final List<Map<String, Object>> list = integrateInfoVOList.stream().map(integrateInfo -> BeanUtil.beanToMap(integrateInfo)).collect(Collectors.toList());
        page2.setRecords((List)new IntegrateInfoWrapper(list).wrap());
        return LayuiPageFactory.createPageInfo(page2);
    }
    
    @RequestMapping({ "/addItem" })
    @ResponseBody
    public Object add(final IntegrateInfo integrateInfo) {
        integrateInfo.setCreateTime(new Date());
        ShiroUser user = ShiroKit.getUser();
        integrateInfo.setUserId(user.getId());
        String areaNext = this.getPara("areaNext");
        if(StrUtil.isNotBlank(areaNext)){
            Region region = this.regionService.lambdaQuery().eq(Region::getLongcode, areaNext).one();
            integrateInfo.setAreaId(region.getId());
            integrateInfo.setArea(Utils.areaFullName(areaNext,regionService));
            integrateInfo.setAreaCode(areaNext);
        }else{
            Region regionByNames = this.regionService.getRegionByNames(user.getArea());
            if(regionByNames!=null) {
                integrateInfo.setAreaCode(regionByNames.getLongcode());
            }
        }
        LambdaQueryChainWrapper<IntegrateInfo> integrateInfoLambdaQueryChainWrapper = this.integrateInfoService.lambdaQuery()
                .eq(IntegrateInfo::getAreaCode, areaNext)
                .nested(i->i.eq(IntegrateInfo::getWasteTypeId,integrateInfo.getWasteTypeId()).or().isNull(IntegrateInfo::getWasteTypeId));
        List<IntegrateInfo> existList = integrateInfoLambdaQueryChainWrapper.list();
        if(existList.size()>0){
            throw new ServiceException(500,"该地区的积分已设置！");
        }else {
            this.integrateInfoService.save(integrateInfo);
        }
        return IntegrateInfoController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/delete" })
    @ResponseBody
    public Object delete(@RequestParam final String id) {
        this.integrateInfoService.removeById((Serializable)id);
        return IntegrateInfoController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/editItem" })
    @ResponseBody
    public Object update(final IntegrateInfo integrateInfo) {
        this.integrateInfoService.updateById(integrateInfo);
        return IntegrateInfoController.SUCCESS_TIP;
    }
    
    @RequestMapping(value = { "/delItems" }, method = { RequestMethod.POST })
    @ResponseBody
    public Object delItem(@RequestBody final String[] ids) {
        this.integrateInfoService.removeByIds((Collection)Arrays.asList(ids));
        return IntegrateInfoController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/show" })
    public Object show(final String id, final Model model) {
        final IntegrateInfo integrateInfo = (IntegrateInfo)this.integrateInfoService.getById((Serializable)id);
        final Map map = BeanUtil.beanToMap(integrateInfo);
        model.addAttribute("integrateInfo", new IntegrateInfoWrapper(map).wrap());
        return this.PREFIX + "integrateInfo_show.html";
    }
}
